const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const GoodImage = sequelize.define('GoodImage', {
  id: {
    type: DataTypes.INTEGER.UNSIGNED,
    primaryKey: true,
    autoIncrement: true
  },
  goodsId: {
    type: DataTypes.INTEGER.UNSIGNED,
    allowNull: false,
    field: 'goods_id'
  },
  imageUrl: {
    type: DataTypes.STRING(255),
    allowNull: false,
    field: 'img_url'
  },
  description: {
    type: DataTypes.STRING(255),
    allowNull: true
  },
  sortOrder: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    field: 'sort_order'
  }
}, {
  tableName: 'good_images',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at'
});

// 添加实例方法以确保字段正确序列化
GoodImage.prototype.toJSON = function() {
  const values = Object.assign({}, this.get());
  // 移除时间字段
  delete values.created_at;
  delete values.updated_at;
  return values;
};

module.exports = GoodImage;